//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js');
var key = app.globalData.key;
var resurl = app.globalData.resurl
let interstitialAd = null;
Page({
  insertScreenAdTimer: '',
  data: {
    isReview: '',
    isReview1: '',
    indexCon:'',
    bannerShow: true,
    show:false,
    showMask: false,
    adverShow: true,
    myId: '',
    money: '',
    countOne: 5,
    countTwo: 5,
    countThr: 5,
    opacity: 1,
    showToast: false,
    imgShow: false,
    condition:['今日提现额度已达上限！','余额不足！','连续登录天数不足！','邀请人数不足！','看视频次数不足!','提现人员已满！','操作频繁','提现失败'],
    arrList: [
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 1,
        haveNum: '',
        needDay: '',
        videoNum: '',
        needManNum: '',
        isExtract: '',
      },
      {
        quantity: '',
        headTitle: '',
        tailTitle: '',
        id: 3,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      },
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 5,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      }
    ],
    arrListTwo: [
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 7,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      },
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 9,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      },
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 11,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      }
    ],
    arrListThree: [
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 12,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      },
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 13,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      },
      {
        quantity: '',
        headTitle: '',
        tailTitle: "",
        id: 14,
        needDay: '',
        videoNum: '',
        needManNum: '',
        haveNum: '',
        isExtract: '',
      }
    ],
    desList: [
      '1、单笔提现最低0.3元，最高100元，满足条件即可实时提现到账',
      '2、100元是无门槛提现，养猪余额满100元即可提现，其他额度达到条件就能够提现',
      '3、如提示超过当日提现额度上限，请明日再来，提现额度每日0点更新',
      '4、如有产品提现、使用和反馈建议，请点击联系客服，我们看到留言会尽快回复',
      '5、金猪生打钱拥有最终解释权，官方拒绝一切作弊行为',
    ],
    src: '/images/image_item_choose.png',
    showOne: false,
    showtwo: false,
    showThree: false,
    loginName: '',
    touxiang: '',
  },
  //事件处理函数
  onLoad: function () {
    var that = this;
  },
  //复制id
  copyText: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.id,
      success: function () {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  nowGet: function (e) {
    var that = this;
    var arrList = e.currentTarget.dataset.arrlist;
    var arrListTwo = e.currentTarget.dataset.arrlisttwo;
    var arrListThree = e.currentTarget.dataset.arrlistthree;
    var countOne = e.currentTarget.dataset.countone;
    var countTwo = e.currentTarget.dataset.counttwo;
    var countThr = e.currentTarget.dataset.countthr;
    var money = that.data.money;
    var idArr = [1, 3, 5, 7, 9, 11, 12, 13, 14];
    var countOneId = idArr.slice(0, 3);
    var countTwoId = idArr.slice(3, 6);
    var countThrId = idArr.slice(6, 9);
    var withdrawalId = countOneId[countOne] || countTwoId[countTwo] || countThrId[countThr];
    console.log(withdrawalId);
    // console.log(arrListTwo);
    var userId = wx.getStorageSync("userId");
    var withdrawalMoney = [0.3, 2, 3, 0.3, 0.8, 5, 10, 100, 0.3];
    var countOneMy = withdrawalMoney.slice(0, 3);
    var countTwoMy = withdrawalMoney.slice(3, 6);
    var countThrMy = withdrawalMoney.slice(6, 9);
    var costMoney = countOneMy[countOne] || countTwoMy[countTwo] || countThrMy[countThr];
    // console.log(costMoney*10000);

    var data18 = {
      deviceProperties: app.globalData.deviceProperties,
      extractKind: {
        userId: userId,
        id: withdrawalId,
      }
    }
    data18 = JSON.stringify(data18);
    data18 = util.base64_encode(data18);
    data18 = util.xxtea_encrypt(data18, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=18&a=1&b=2',
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      data: data18,
      method: "POST",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          that.setData({
            showToast: true,
            opacity: 0.5,
            money: money - costMoney * 10000,
          })
        } else if (res.resultCode === -8) {
          that.setData({
            indexCon: 0,
          })
          that.falseContion();
        } else if (res.resultCode === -10) {
          that.setData({
            indexCon: 1,
          })
          that.falseContion();
        } else if (res.resultCode === -11) {
          that.setData({
            indexCon: 2,
          })
          that.falseContion();
        } else if (res.resultCode === -12) {
          that.setData({
            indexCon: 3,
          })
          that.falseContion();
        } else if (res.resultCode === -13) {
          that.setData({
            indexCon: 4,
          })
          that.falseContion();
        } else if (res.resultCode === -14) {
          that.setData({
            indexCon: 5,
          })
          that.falseContion();
        } else if (res.resultCode === -7) {
          that.setData({
            indexCon: 6,
          })
          that.falseContion();
        }
        else if (res.resultCode === -6) {
          that.setData({
            indexCon: 7,
          })
          that.falseContion();
        }
      }
    })

  },
  closeToast: function () {
    this.setData({
      //opacity:1,
      showToast: false,
    })
  },
  clickOne: function (e) {
    var that = this;
    let index = e.currentTarget.dataset.index;
    that.setData({
      countTwo: 5,
      countThr: 5,
      countOne: index,
      showOne: true,
      showTwo: false,
      showThree: false,
      imgShow: true
    })
    if (that.data.countOne === 0) {
      var id = 1;
    } else if (that.data.countOne === 1) {
      var id = 3;
    } else {
      var id = 5;
    }
    var userId = wx.getStorageSync("userId");
    var data17 = {
      deviceProperties: app.globalData.deviceProperties,
      extractKind: {
        userId: userId,
        id: id,
      }
    }
    data17 = JSON.stringify(data17);
    data17 = util.base64_encode(data17);
    data17 = util.xxtea_encrypt(data17, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=17&a=1&b=2',
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      data: data17,
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          console.log(res.haveNum);
          console.log(id);
          var arrList = that.data.arrList
          arrList[index].haveNum = res.haveNum;
          arrList[index].isExtract = res.isExtract;
          that.setData({
            arrList,
          })
        }
      }
    })



  },

  clickTwo: function (e) {
    let index = e.currentTarget.dataset.index;
    var that = this;
    that.setData({
      countOne: index,
    })
    if (that.data.countOne === 0) {
      var id = 7;
    } else if (that.data.countOne === 1) {
      var id = 9;
    } else {
      var id = 11;
    }
    var userId = wx.getStorageSync("userId");
    var data17 = {
      deviceProperties: app.globalData.deviceProperties,
      extractKind: {
        userId: userId,
        id: id,
      }
    }
    data17 = JSON.stringify(data17);
    data17 = util.base64_encode(data17);
    data17 = util.xxtea_encrypt(data17, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=17&a=1&b=2',
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      data: data17,
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          console.log(res.haveNum);
          console.log(id);
          var arrListTwo = that.data.arrListTwo
          arrListTwo[index].haveNum = res.haveNum;
          arrListTwo[index].isExtract = res.isExtract;
          that.setData({
            arrListTwo,
          })
        }
      }
    })
    that.setData({
      countOne: 5,
      countThr: 5,
      countTwo: index,
      showOne: false,
      showTwo: true,
      showThree: false
    })
  },
  clickThree: function (e) {
    var that = this;
    let index = e.currentTarget.dataset.index;
    that.setData({
      countOne: index,
    })
    if (that.data.countOne === 0) {
      var id = 12;
    } else if (that.data.countOne === 1) {
      var id = 13;
    } else {
      var id = 14;
    }
    var userId = wx.getStorageSync("userId");
    var data17 = {
      deviceProperties: app.globalData.deviceProperties,
      extractKind: {
        userId: userId,
        id: id,
      }
    }
    data17 = JSON.stringify(data17);
    data17 = util.base64_encode(data17);
    data17 = util.xxtea_encrypt(data17, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=17&a=1&b=2',
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      data: data17,
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          console.log(res.haveNum);
          console.log(id);
          var arrListThree = that.data.arrListThree
          arrListThree[index].haveNum = res.haveNum;
          arrListThree[index].isExtract = res.isExtract;
          that.setData({
            arrListThree,
          })
        }
      }
    })
    that.setData({
      countOne: 5,
      countTwo: 5,
      countThr: index,
      showOne: false,
      showTwo: false,
      showThree: true
    })
  },
falseContion: function() {
  this.setData({
    show:true,
    showMask: true,
  })
},
  close:function() {
    this.setData({
      show:false,
      showMask: false,
      bannerShow: true,
    })
  },
  closeBanner: function () {
    this.setData({
      bannerShow: false,
    })
  },
  onShow: function () {
    var that = this;
    var isReview = wx.getStorageSync("isReview");
    var isReview1 = wx.getStorageSync("isReview1");
    var money = app.globalData.money;
    var myRestMoney = Math.floor(money / 10000 * 100) / 100;
    that.setData({
      money: app.globalData.money,
      myRestMoney: myRestMoney,
      isReview: isReview,
      isReview1: isReview1,
    })

    //interface 16
    var userId = wx.getStorageSync("userId");
    var data16 = {
      deviceProperties: app.globalData.deviceProperties,
      extractKind: {
        userId: userId,
      }
    }
    data16 = JSON.stringify(data16);
    data16 = util.base64_encode(data16);
    data16 = util.xxtea_encrypt(data16, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=16&a=1&b=2',
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      data: data16,
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        // console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          var exKinds = res.extractKinds;
          var exKindsOne = exKinds.slice(0, 3);
          var exKindsTwo = exKinds.slice(3, 6);
          var exKindsThree = exKinds.slice(6, 9);
          var arrList = that.data.arrList;
          var arrListTwo = that.data.arrListTwo;
          var arrListThree = that.data.arrListThree;
          for (var i = 0; i < 3; i++) {
            arrList[i].quantity = exKindsOne[i].money / 10000;
            arrList[i].headTitle = exKindsOne[i].headTitle;
            arrList[i].tailTitle = exKindsOne[i].tailTitle;
            arrList[i].needDay = exKindsOne[i].needDay;
            arrList[i].videoNum = exKindsOne[i].videoNum;
            arrList[i].needManNum = exKindsOne[i].needManNum;
          }
          for (var j = 0; j < 3; j++) {
            arrListTwo[j].quantity = exKindsTwo[j].money / 10000;
            arrListTwo[j].headTitle = exKindsTwo[j].headTitle;
            arrListTwo[j].tailTitle = exKindsTwo[j].tailTitle;
            arrListTwo[j].needDay = exKindsTwo[j].needDay;
            arrListTwo[j].videoNum = exKindsTwo[j].videoNum;
            arrListTwo[j].needManNum = exKindsTwo[j].needManNum;
          }
          for (var k = 0; k < 3; k++) {
            arrListThree[k].quantity = exKindsThree[k].money / 10000;
            arrListThree[k].headTitle = exKindsThree[k].headTitle;
            arrListThree[k].tailTitle = exKindsThree[k].tailTitle;
            arrListThree[k].needDay = exKindsThree[k].needDay;
            arrListThree[k].videoNum = exKindsThree[k].videoNum;
            arrListThree[k].needManNum = exKindsThree[k].needManNum;
          }
          that.setData({
            arrList: arrList,
            arrListTwo: arrListTwo,
            arrListThree: arrListThree,
          })
          // console.log(arrListThree)
        }
      }
    })
    that.getUserInfo();
    var count = 0;
    var insertScreenAdTimer = setInterval(function () {
      count++;
      console.log(count);
      if (count > 3) {
        that.insertScreenAD();
        clearInterval(insertScreenAdTimer);
      }
    }, 1000)
    that.insertScreenAdTimer = insertScreenAdTimer;
  },
  insertScreenAD: function () {
    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-1cc4cb154d00aa08'
      })
      interstitialAd.onLoad(() => { })
      interstitialAd.onError((err) => { })
      interstitialAd.onClose(() => { })
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  connected: function () {
    wx.navigateTo({
      url: '/pages/connecting/connecting',
    })
  },
  back: function () {
    wx.navigateBack({
      delta: 1
    });

  },

  // 获取用户信息头像Id之类的
  getUserInfo: function () {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
      }
    }
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    //获取用户信息接口
    wx.request({
      url: resurl + '/interface/sdkData?requestId=3&a=1&b=2',
      data: _data,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          var loginName = unescape(res.user.realName);
          that.setData({
            touxiang: res.user.userLogo,
            loginName: loginName,
            myId: res.user.userId,
            money: res.user.money,
          })
        }
      }
    })
  },

  /**
    * 开始旋转
    */


  /**
    * 停止旋转
    */





  /**
     * 生命周期函数--监听页面隐藏
     */
  onHide: function () {
    var that = this;
    clearInterval(that.insertScreenAdTimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(that.insertScreenAdTimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})
